<?php

/**
 * Controller_ware
 * 档案管理
 */

class Controller_ware extends MyApp {

	public function __construct()
	{

		parent::__construct();

		$this->model->file   = YOU::loadModel('file');
		$this->model->user   = YOU::loadModel('user');
		$this->model->ware   = YOU::loadModel('ware');

		// 左侧文件夹展开
		$folder_left = $this->db->fetchAll("SELECT * FROM `tbl_ware_folder`");
		$this->tpl->assign("folder_left", $folder_left);

	}
	/** 首页 **/
	public function actionIndex()
	{

	}

	/** 档案管理展示页 **/
	public function actionList ()
	{	
		// 上传人
		$user_lists = $this->db->fetchAll("SELECT  distinct user_id FROM `tbl_ware`");
		
		foreach ($user_lists as $key => $value){
			$user_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
		};
		
		
		// 分类
		$type_id		= $this->get->type_id;
		// 目录
		$folder_id		= $this->get->folder_id;
		// 查询条件
		$ware_title		= $this->get->ware_title;
		$user_id		= $this->get->user_id;
		$from			= $this->get->from;
		$to				= $this->get->to;

		$where = " WHERE 1=1 ";

		// 进入分类入口
		if(!empty($type_id)){
			$where .= " AND type_id ='{$type_id}' ";
		}
		// 进入目录入口
		if(!empty($folder_id)){
			$where .= " AND folder_id ='{$folder_id}' ";
		}

		if(!empty($ware_title)){
			// 标题
			$where .= " AND ware_title LIKE '%{$ware_title}%' ";
			$this->tpl->assign("ware_title", $ware_title);

		}
		
		if(!empty($user_id)){
			
			// 上传人
			$where .= " AND user_id ='{$user_id}' ";
			$this->tpl->assign("user_id", $user_id);
		}
		if(!empty($from)){
			// 开始时间
			$where .= " AND create_time >= '{$from}' ";
			$this->tpl->assign("from", $from);
		}
		if(!empty($to)){
			// 结束时间
			$where .= " AND create_time <= '{$to}+1' ";
			$this->tpl->assign("to", $to);
		}

		// 管理分类名称
		$type_lists = $this->model->ware->getTypeAll();
		// 获取分类名称
		$folder_data = $this->model->ware->getFolderInfo($folder_id);
		// 分页tbl_ware_folder
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_ware` {$where}");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}

		$ware_lists = $this->db->fetchAll("SELECT * FROM `tbl_ware` {$where} ORDER BY ware_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		foreach($ware_lists as $key=>$value){
			// 分类名称
			$type_name= $this->db->fetchRow("SELECT `type_name` FROM `tbl_ware_type`  WHERE type_id ={$value['type_id']}");
			$ware_lists[$key]['type_name']=$type_name['type_name'];
			// 提交者
			$ware_lists[$key]['user_name'] = $this->model->user->getNameById($value['user_id']);
			// 时间转换
			$ware_lists[$key]['create_time'] = date('Y-m-d',strtotime($value['create_time']));
		}

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("ware_lists", $ware_lists);
		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->assign("user_lists", $user_lists);
		$this->tpl->assign("folder_id", $folder_id);
		
		$this->tpl->assign("folder_data", $folder_data);
		$this->tpl->display('ware/list.tpl.php');

	}
	/** -------------------------- 档案内容 --------------------------- **/
	/** 内容添加 **/
	public function actionAdd()
	{
		// 目录
		$folder_id		= $this->get->folder_id;
		// 管理分类名称
		$type_lists = $this->db->fetchAll("SELECT * FROM `tbl_ware_type`");
		if($_POST){

			$ware_title     		= $this->post->ware_title;
			$type_id     			= $this->post->type_id;
			$ware_from     			= $this->post->ware_from;
			$ware_content     		= $this->post->ware_content;

			// 写入数据库
			$info = array(

			"ware_title"       		=> $ware_title,
			"type_id"       		=> $type_id,
			"ware_from"       		=> $ware_from,
			"ware_content"       	=> $ware_content,
			"folder_id"       		=> $folder_id,
			"user_id"       		=> $this->user['user_id'],
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_ware", $info);
			$ware_id = $this->db->insert_id();
			// 附件上传

			$file = $this->model->file->upload("ware_file", $ware_id, array("txt","ppt", "xls", "xlsx", "docs", "docx","doc", "pdf", "jpg", "png", "gif", "zip",
			"rar"));
			if (!empty($file)){
				$info = array(
				"ware_file" => $file['fname'],
				);

				$this->db->do_update("tbl_ware", $info, "ware_id='{$ware_id}'");
			}

			Helper_Client::location("添加成功", "index.php?a=ware.list&folder_id={$folder_id}");
		}

		$this->tpl->assign("folder_id", $folder_id);
		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->display("ware/add.tpl.php");

	}
	/** 内容修改 **/
	public function actionUpdate()
	{
		// 目录
		$folder_id	= $this->get->folder_id;
		// 管理分类名称
		$type_lists = $this->db->fetchAll("SELECT * FROM `tbl_ware_type`");

		$ware_id	= (int)$this->get->ware_id;
		$ware_data	= $this->db->fetchRow("SELECT * FROM `tbl_ware` WHERE `ware_id`='{$ware_id}'");
		if (empty($ware_data)){
			Helper_Client::alert("内容不存在");
		}

		if ($_POST){

			$ware_title     		= $this->post->ware_title;
			$type_id     			= $this->post->type_id;
			$ware_from     			= $this->post->ware_from;
			$ware_content     		= $this->post->ware_content;

			// 写入数据库
			$info = array(

			"ware_title"       		=> $ware_title,
			"type_id"       		=> $type_id,
			"ware_from"       		=> $ware_from,
			"ware_content"       	=> $ware_content,
			"folder_id"       		=> $folder_id,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_ware", $info, "ware_id='{$ware_id}'");
			// 附件上传
			if ($this->post->ware_file_re=='1'){
			
				// 附件上传
				$file = $this->model->file->upload("ware_file", $ware_id, array("txt","ppt", "xls", "xlsx", "docs", "doc", "pdf", "jpg", 
"png", "gif", "zip", "rar"));
				
				if (!empty($file)){
					$info = array(
					"ware_file" => $file['fname'],
					);

					$this->db->do_update("tbl_ware", $info, "ware_id='{$ware_id}'");
				}
			}

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=ware.list&folder_id={$folder_id}");
		}

		$this->tpl->assign("ware_data", $ware_data);
		$this->tpl->assign("folder_id", $folder_id);
		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->display('ware/update.tpl.php');
	}
	/** 内容查看 **/
	public function actionDetail()
	{
		// 目录
		$folder_id	= $this->get->folder_id;
		$ware_id	= (int)$this->get->ware_id;

		$ware_data	= $this->db->fetchRow("SELECT * FROM `tbl_ware` WHERE `ware_id`='{$ware_id}'");
		// 提交者
		$ware_data['user_name'] = $this->model->user->getNameById($ware_data['user_id']);
		// 时间转换
		$ware_data['create_time'] = date('Y-d-m',strtotime($ware_data['create_time']));
		// 分类名称
		$type_name= $this->db->fetchRow("SELECT * FROM `tbl_ware_type` WHERE type_id ={$ware_data['type_id']}");
		$ware_data['type_name']=$type_name['type_name'];

		if (empty($ware_data)){
			Helper_Client::alert("内容不存在");
		}



		$this->tpl->assign("ware_data", $ware_data);
		$this->tpl->assign("folder_id", $folder_id);
		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->display('ware/detail.tpl.php');
	}
	/** 内容删除 **/
	public function actionDel()
	{
		// 目录
		$folder_id	= $this->get->folder_id;
		$ware_id	= (int)$this->get->ware_id;
		$this->db->query("DELETE  FROM `tbl_ware` WHERE ware_id='{$ware_id}'");

		// 跳转
		Helper_Client::location("修改成功", "index.php?a=ware.list&folder_id={$folder_id}");
	}

	/** ---------------------------------- 档案管理--分类 ------------------------------- **/
	/** 档案管理分类列表 **/
	public function actionTypeList ()
	{

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_ware_type`");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$type_lists = $this->db->fetchAll("SELECT * FROM `tbl_ware_type`  ORDER BY type_id DESC LIMIT {$start}, ".PAGE_LIMIT."");

		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("type_lists", $type_lists);
		$this->tpl->display('ware/type.list.tpl.php');

	}
	/** 档案管理分类添加 **/
	public function actionTypeAdd()
	{
		if ($_POST){

			$type_name     			= $this->post->type_name;

			// 写入数据库
			$info = array(

			"type_name"       		=> $type_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_ware_type", $info);

			Helper_Client::location("添加成功", "index.php?a=ware.typelist");
		}

		$this->tpl->display("ware/type.add.tpl.php");

	}
	/** 档案管理分类修改**/
	public function actionTypeUpdate()
	{

		$type_id   = (int)$this->get->type_id;
		$type_data = $this->db->fetchRow("SELECT * FROM `tbl_ware_type` WHERE `type_id`='{$type_id}'");
		if (empty($type_data)){
			Helper_Client::alert("分类名成不存在");
		}

		if ($_POST){

			$type_name   	    = $this->post->type_name;

			// 写入数据库
			$info = array(

			"type_name"				=> $type_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_ware_type", $info, "type_id='{$type_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=ware.typelist ");
		}

		$this->tpl->assign("type_data", $type_data);
		$this->tpl->display('ware/type.update.tpl.php');
	}
	/** 档案管理分类删除 **/
	public function actionTypeDel()
	{
		$type_id = $this->get->type_id;
		$this->db->query("DELETE  FROM `tbl_ware_type` WHERE type_id='{$type_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=ware.typelist");
	}
	/** --------------------------------------档案管理--目录---------------------------------- **/
	/** 档案管理目录列表 **/
	public function actionFolderList ()
	{

		// 分页
		YOU::loadLib('page', "", '0');
		$tmp = $this->db->fetchRow("SELECT COUNT(1) AS count FROM `tbl_ware_folder` ");
		$count = $tmp['count'];
		$page_total = ceil($count/PAGE_LIMIT);
		// 当前页
		$p = intval($_GET['p']);
		if ($p == 0){
			$p  = 1;
		}
		if ($p > 1){
			$start = PAGE_LIMIT * ($p-1);
		}else{
			$start = 0;
		}
		$folder_lists = $this->db->fetchAll("SELECT * FROM `tbl_ware_folder`  ORDER BY folder_id DESC LIMIT {$start}, ".PAGE_LIMIT."");


		$page = new Page(array('total' => $count, 'perpage' => PAGE_LIMIT));
		$page->next_page   = '下一页>';
		$page->pre_page    = '<上一页';
		$page->style_path  = "static/page_style";
		$this->tpl->assign("page", $page->show(4, "yahoo"));

		$this->tpl->assign("folder_lists", $folder_lists);
		$this->tpl->display('ware/folder.list.tpl.php');

	}
	/** 档案管理目录添加 **/
	public function actionFolderAdd()
	{
		if ($_POST){

			$folder_name     		= $this->post->folder_name;

			// 写入数据库
			$info = array(

			"folder_name"       	=> $folder_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);

			$this->db->do_insert("tbl_ware_folder", $info);

			Helper_Client::location("添加成功", "index.php?a=ware.folderlist");
		}

		$this->tpl->display("ware/folder.add.tpl.php");
	}
	/** 档案管理目录修改**/
	public function actionFolderUpdate()
	{

		$folder_id   = (int)$this->get->folder_id;
		$folder_data = $this->db->fetchRow("SELECT * FROM `tbl_ware_folder` WHERE `folder_id`='{$folder_id}'");
		if (empty($folder_data)){
			Helper_Client::alert("目录名成不存在");
		}

		if ($_POST){

			$folder_name   	    = $this->post->folder_name;

			// 写入数据库
			$info = array(

			"folder_name"			=> $folder_name,
			"status"                => 1,
			"create_time"           => date("Y-m-d H:i:s"),
			"update_time"           => "0000-00-00 00:00:00",

			);
			$this->db->do_update("tbl_ware_folder", $info, "folder_id='{$folder_id}'");

			// 跳转
			Helper_Client::location("修改成功", "index.php?a=ware.folderlist ");
		}

		$this->tpl->assign("folder_data", $folder_data);
		$this->tpl->display('ware/folder.update.tpl.php');
	}
	/** 档案管理分类 **/
	public function actionFolderDel()
	{
		$folder_id = $this->get->folder_id;
		$this->db->query("DELETE  FROM `tbl_ware_folder` WHERE folder_id='{$folder_id}'");

		// 跳转
		Helper_Client::location("删除成功", "index.php?a=ware.folderlist");
	}
	/** 下载**/
	public function actionoutput(){
		$fname = $this->get->ware_file;
		$fpath = UPLOAD_PATH."/ware_file/{$fname}";

		// 输出下载文件头s
		header("Content-Disposition: attachment; filename={$fname}");
		// 在线输出
		readfile($fpath);

	}


}

?>